package org.come.good;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.come.Frame.ZhuFrame;
import org.come.bean.LoginResult;
import org.come.bean.PackGiftBean;
import org.come.entity.Goodstable;
import org.come.mouslisten.GoodsMouslisten;
import org.come.mouslisten.PetAddPointMouslisten;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.AnalysisString;
import org.come.until.EquipTool;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.GsonUtil;
import org.come.until.UserMessUntil;

import com.tool.PlayerKill.PKSys;
import com.tool.image.ImageMixDeal;
import com.tool.role.RoleData;
import com.tool.role.RoleProperty;

/**
 * 礼盒类使用
 *
 * @author Administrator
 *
 */
public class Box {
	/**
	 * 新手礼包使用
	 */
	public static void Novice(Goodstable goodstable) {
		List<Goodstable> goodList = new ArrayList<>();
		String[] v = goodstable.getValue().split("\\|");
		for (int i = 0; i < v.length; i++) {
			String[] v2 = v[i].split("=");
			if ("等级要求".equals(v2[0])) {
				if (!AnalysisString.lvlfull(ImageMixDeal.userimg.getRoleShow().getGrade(), v2[1])) {
					ZhuFrame.getZhuJpanel().addPrompt2("你等级太低");
					return;
				}
			} else if ("物品".equals(v2[0])) {
				String[] v3 = v2[1].split("\\&");
				for (int j = 0; j < v3.length; j++) {
					String[] v4 = v3[j].split("\\$");
					Goodstable addgood = UserMessUntil.getgoodstable(new BigDecimal(v4[0]));
					if (addgood != null) {
						if (EquipTool.isEquip(addgood.getType())) {
							int vs = Integer.parseInt(v4[1]);
							for (int k = 0; k < vs; k++) {
								addgood.setRole_id(ImageMixDeal.userimg.getRoleShow().getRole_id());
								addgood.setUsetime(1);
								goodList.add(addgood);
							}
						} else {
							addgood.setRole_id(ImageMixDeal.userimg.getRoleShow().getRole_id());
							addgood.setUsetime(Integer.parseInt(v4[1]));
							goodList.add(addgood);
						}

					}
				}
			}
		}
		if (goodList.size() == 0) {
			return;
		}
		if (GoodsListFromServerUntil.Surplussum("-1", "-1", goodList.size()) != goodList.size()) {
			ZhuFrame.getZhuJpanel().addPrompt2("你的背包不够");
			return;
		}
		goodstable.setUsetime(0);
		PackGiftBean bean = new PackGiftBean();
		List<String> goods=new ArrayList<>();
		for (int i = 0; i < goodList.size(); i++) {
			Goodstable good=goodList.get(i);
			if (good.getType()==60001||good.getType()==60002) {
				goodstable.setUsetime(1);
				goodstable.setValue(good.getValue());
				goodstable.setType(good.getType());
			}else {
				goods.add(good.getGoodsid()+"|"+good.getUsetime());
			}
			good.setUsetime(1);
		}
		ZhuFrame.getZhuJpanel().addPrompt2("你的使用了" + goodstable.getGoodsname());
		bean.setGoodstable(goodstable);
		bean.setGoods(goods);
		String mes = Agreement.getAgreement().packgiftAgreement(GsonUtil.getGsonUtil().getgson().toJson(bean));
		// 向服务器发送信息
		SendMessageUntil.toServer(mes);
	}
	// 特赦令使用
	public static void tesheling(Goodstable goodstable) {
		PKSys pkSys = PKSys.getPkSys();
		if (pkSys.getPk1() > 0) {
			pkSys.expiation();
		} else {
			ZhuFrame.getZhuJpanel().addPrompt2("你身上没有PK点,就不要瞎捣乱,特赦令就不还给你了");
		}
		goodstable.goodxh(1);
		GoodsMouslisten.gooduse(goodstable, 1);
	}
	/** 2114超级星梦石头使用 */
	public static void xms(Goodstable goodstable) {
		Goodstable[] Goodstables = GoodsListFromServerUntil.getChoseGoodsList();
		for (int i = 0; i < Goodstables.length; i++) {
			if (Goodstables[i] != null) {
				ZhuFrame.getZhuJpanel().addPrompt2("请脱下装备");
				return;
			}
		}
		LoginResult loginResult = RoleData.getRoleData().getLoginResult();
		int lvl = AnalysisString.lvlint(loginResult.getGrade());
		loginResult.setBone(lvl);
		loginResult.setSpir(lvl);
		loginResult.setPower(lvl);
		loginResult.setSpeed(lvl);
		if (loginResult.getTurnAround() >= 4) {
			loginResult.setCalm(lvl);
		}
		loginResult.setHp(new BigDecimal(RoleProperty.getHp(loginResult)));
		loginResult.setMp(new BigDecimal(RoleProperty.getMp(loginResult)));
		ZhuFrame.getZhuJpanel().addPrompt2("你使用了" + goodstable.getGoodsname());
		goodstable.setUsetime(goodstable.getUsetime() - 1);
		GoodsMouslisten.gooduse(goodstable, 1);
		// 刷新面板
		PetAddPointMouslisten.getplayerValue();
		String mes = Agreement.getAgreement().rolechangeAgreement("D"+loginResult.getBone()+"="+loginResult.getSpir()+"="+loginResult.getPower()+"="+loginResult.getSpeed()+"="+loginResult.getCalm());
		SendMessageUntil.toServer(mes);
	}
}
